﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;


namespace computerbookwebsite
{
    public partial class reg : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnReg_Click(object sender, EventArgs e)
        {
            #region //1.检测用户名是否已经存在
            //1.1创建连接对象
            SqlConnection mycon = new SqlConnection();
            mycon.ConnectionString = ConfigurationManager.ConnectionStrings["bookstoreConnectionString"].ConnectionString;
            mycon .Open();
            //1.2创建命令对象
            SqlCommand mycom = new SqlCommand();
            mycom.Connection = mycon;
            mycom.CommandText = string.Format("select*from member where LoginName='{0}' ", txtUserName.Text);
            //1.3执行命令对象：用户是否存在
            SqlDataReader dr = mycom.ExecuteReader();
            if (dr.HasRows)
            {
                //1.4如果存在,则不能注册
                Response.Write("<script>alert('该用户名已经存在，请重新设置用户名！')</script>");
            }
#endregion



            #region 添加新的用户
            //获取用户输入的信息
            string username = txtUserName.Text;
            string pwd = txtPassword.Text;
            string regdate = DateTime.Now.ToShortDateString();
            String strsql = string.Format("insert into member values(0,'{0}','{1}','{2}','',0,'')", username, pwd, regdate);
            //1.创建连接对象
            SqlConnection cn = new SqlConnection();
            cn.ConnectionString = ConfigurationManager.ConnectionStrings["bookstoreConnectionString"].ConnectionString;
            cn.Open();

            //2.创建命令对象
            SqlCommand cm = new SqlCommand();
            cm.Connection = cn;
            cm.CommandText = strsql;

            //3.执行命令对象
            if (cm.ExecuteNonQuery()>0)
            Response.Write("<script>alert('注册成功!')</script>");
#endregion
        }
    }
}